package com.zl.quickdoc.resolver;

import java.util.HashMap;
import java.util.Map;

import com.zl.quickdoc.resolver.converter.JavaParserTagConverter;
import com.zl.quickdoc.resolver.converter.impl.ContentTypeTagConverter;
import com.zl.quickdoc.resolver.converter.impl.DataTypeTagConverter;
import com.zl.quickdoc.resolver.converter.impl.DefaultStringParserTagConverterImpl;
import com.zl.quickdoc.resolver.converter.impl.ParamObjTagConverter;
import com.zl.quickdoc.resolver.converter.impl.ParamTagConverter;

/**
 * 注释标签转换器
 *
 * @author zhouliang
 *
 */
public class JavaParserTagConverterRegistrar {
    
    private static JavaParserTagConverterRegistrar INSTANCE = new JavaParserTagConverterRegistrar();
    
    private Map<String, JavaParserTagConverter> registrar = new HashMap<>(6);
    
    /**
     * 默认转换器
     */
    private JavaParserTagConverter defaultTagConverter = new DefaultStringParserTagConverterImpl();

    private JavaParserTagConverterRegistrar() {
        //注册特殊标签的转换器
        registerConverter("@param", new ParamTagConverter());
        registerConverter("@resp", new ParamTagConverter());
        registerConverter("@see", new ParamObjTagConverter());
        registerConverter("@paramObj", new ParamObjTagConverter());
        registerConverter("@contentType", new ContentTypeTagConverter());
        registerConverter("@dataType", new DataTypeTagConverter());
    }
    
    public static JavaParserTagConverterRegistrar getInstance() {
        return INSTANCE;
    }
    
    /**
     * 注册转换器
     *
     * @param tagName      标签
     * @param tagConverter 转换器
     */
    public void registerConverter(String tagName, JavaParserTagConverter tagConverter) {
        registrar.put(tagName, tagConverter);
    }

    /**
     * 获取转换器
     * @param tagName 标签名
     */
    public JavaParserTagConverter getConverter(String tagName) {
        for (Map.Entry<String, JavaParserTagConverter> entry : registrar.entrySet()) {
            if (entry.getKey().equalsIgnoreCase(tagName)) {
                return entry.getValue();
            }
        }
        return defaultTagConverter;
    }
}
